Configuration cServerFolders
{
    param (
	    [Parameter(Mandatory)]
	    [ValidateNotNullOrEmpty()]
	    [PSObject]$ManagedFolder
    )

    Import-DscResource -ModuleName cFileShare
    Import-DscResource -Module PowerShellAccessControl


    $compatibleUniqueKey = $ManagedFolder.uniqueKey.replace(".","").replace("-","")

    #Log Message
    Log "cServerFoldersBegin_$compatibleUniqueKey" {
        Message = "Start cServerFolders for $compatibleUniqueKey"
    } 

    #Ensure folder is present on the managed server
    File "Folder$compatibleUniqueKey" {
        Ensure=$ManagedFolder.Ensure
        Type="Directory"
        DestinationPath = $ManagedFolder.FullPath
    }

    #Write log message to the successfully creation folder
    Log "cServerFoldersSuccessfullCreation_$compatibleUniqueKey" {
        Message = "Folder $compatibleUniqueKey has been successfully created on path $($ManagedFolder.FullPath)"
        DependsOn = "[File]Folder$compatibleUniqueKey"
    } 


    if ($ManagedFolder.Share -and (!($ManagedFolder.Share.contains("$"))))
    {
        cCreateFileShare "Folder$compatibleUniqueKey"
        {
            ShareName = $ManagedFolder.Share
            Path      = $ManagedFolder.FullPath
            Ensure    = $ManagedFolder.Ensure
            DependsOn = "[File]Folder$compatibleUniqueKey"
        }

        cSetSharePermissions "SetPermissions$compatibleUniqueKey"
        {
            ShareName         = $ManagedFolder.Share
            DependsOn         = "[cCreateFileShare]Folder$compatibleUniqueKey"
            Ensure	          = $ManagedFolder.Ensure
            FullAccessUsers   = @('Everyone')
        }

        
    }


    if ($ManagedFolder.Rights)
    {
        foreach($right in $ManagedFolder.Rights)
        {
            #$compatibleUniqueKey = $ManagedFolder.uniqueKey.replace(".","").replace("-","")
            # Here's where resource provider to control modifying ACL protection would go
            cAccessControlEntry "Right_$($right.UniqueKey)"
            {
                Ensure = $right.Ensure
                Path = $right.Path
                AceType = $right.AccessType
                ObjectType = "Directory"
                AccessMask = $right.AccessMask
                Principal = $right.principal
                DependsOn = "[File]Folder$compatibleUniqueKey"
            }
        }

    }


    #Log Message
    Log "cServerFoldersEnd_$compatibleUniqueKey" {
        Message = "End cServerFolders for $compatibleUniqueKey"
    } 

}